//DAU
"use strict"
const mvc = require("cosjs.mvc");
const dbase = require("../dbase");

class dau extends dbase.branch.mongodb {
    constructor(branch,sdk,sid) {
        super(branch, 'dau',true);
        this.sdk = sdk;
        this.sid = sid;
        this.ObjectID = ObjectID.bind(this);
    }
    mget(STime,ETime,keys) {
        let query = {sdk:this.sdk,sid:this.sid,time:{}};
        query['time']['$gte'] = mvc.library("time/sign",STime)
        query['time']['$lte'] = mvc.library("time/sign",ETime)
        let option = {};
        option['sort'] = {"time":-1};
        option['multi'] = true;
        option['fields'] = this.fields(keys);
        option['dataType'] = 'array';
        return this.find(query,option);
    }
    incr(time,key,val) {
        let sign = mvc.library("time/sign",time)
        let query = this.query(sign);
        let update = {"$inc":this.values(key,val)};
        let option = {"upsert": true,"multi":false};
        update['$setOnInsert'] = {"sdk": this.sdk,"sid":this.sid, "time": sign};
        return this.update(query,update,option);
    }
}

module.exports = function(branch,sdk,sid){
    return new dau(branch,sdk,sid);
}


function ObjectID(sign) {
    if(typeof sign === "number"){
        return [this.sdk,this.sid,sign].join("-");
    }
    else{
        return sign
    }
}




